package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.Food;
import cn.edu.tju.elm.service.BusinessService;
import cn.edu.tju.elm.service.FoodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/foods")
@Tag(name = "管理商品")
public class FoodController {
    @Autowired
    private UserService userService;

    @Autowired
    private FoodService foodService;

    @Autowired
    private BusinessService businessService;

    @GetMapping("/{id}")
    @Operation(summary = "返回查询到的一条商品记录", method = "GET")
    public HttpResult<Food> getFoodById(@PathVariable Long id){
        Optional<Food> food = foodService.getFoodById(id);
        if(food.isPresent()) {
            return HttpResult.success(food.get());
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    @GetMapping("")
    @Operation(summary = "获取食品列表", description = "获取所有食品或按商家ID筛选食品")
    public HttpResult<List<Food>> getAllFoods(@RequestParam(name = "business", required = false) Long businessId,
                                              @RequestParam(name = "order", required = false) Long orderId){
        if(businessId != null) {
            List<Food> foods = foodService.getFoodsByBusinessId(businessId);
            return HttpResult.success(foods);
        } else {
            List<Food> foods = foodService.getAllFoods();
            return HttpResult.success(foods);
        }
    }

    @PostMapping("")
    @Operation(summary = "添加新食品", description = "添加一个新的食品信息")
    public HttpResult<Food> addFood(@RequestBody Food food){
        try {
            Food savedFood = foodService.saveFood(food);
            return HttpResult.success(savedFood);
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "创建食品失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新食品信息", description = "更新指定ID的食品信息")
    public HttpResult<Food> updateFood(@PathVariable Long id, @RequestBody Food food){
        Food updatedFood = foodService.updateFood(id, food);
        if(updatedFood != null) {
            return HttpResult.success(updatedFood);
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除食品", description = "删除指定ID的食品")
    public HttpResult<Food> deleteFood(@PathVariable Long id){
        boolean deleted = foodService.deleteFood(id);
        if(deleted) {
            return HttpResult.success();
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }
}
